package com.syh.jn.scm.infrastructure.repository.impl.upstream.finance;

import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AbstractAggregateRepository;
import com.syh.jn.scm.domain.upstream.entity.finance.UpReceiptBill;
import com.syh.jn.scm.domain.upstream.repository.finance.UpReceiptBillRepository;
import com.syh.jn.scm.dto.upstream.finance.UpReceiptBillVo;
import com.syh.jn.scm.infrastructure.repository.model.finance.PaymentBillModel;
import com.syh.jn.scm.infrastructure.repository.utils.SQLAliasGenerateUtil;
import com.syh.jn.scm.infrastructure.wrapper.BaseResolve;
import com.syh.jn.scm.infrastructure.wrapper.ResolveToSQL;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

@Repository
public class UpReceiptBillRepositoryImpl extends AbstractAggregateRepository<UpReceiptBill, PaymentBillModel, Long> implements UpReceiptBillRepository {

  @Override
  protected BaseResolve getViewWrapperResolve(Wrapper wrapper) {
    Map<String, String> params = new HashMap<>();
    params.put("id", "upReceiptBill.ID");
    params.put("code", "upReceiptBill.CODE");
    params.put("customerType", "upReceiptBill.CUSTOMER_TYPE");
    params.put("customerId", "upReceiptBill.CUSTOMER_ID");
    params.put("receiptState", "upReceiptBill.RECEIPT_STATE");
    params.put("statementOfAccountId", "upReceiptBill.STATEMENT_OF_ACCOUNT_ID");
    params.put("statementOfAccountCode", "statement.CODE");
    params.put("inputTimestamp", "upReceiptBill.INPUT_TIMESTAMP");
    ResolveToSQL parse = new ResolveToSQL(wrapper, (propertyName) -> {
      return params.get(propertyName);
    });
    return parse;
  }

  @Override
  protected SQL getViewSql() {
    return new SQL() {{
      SELECT(SQLAliasGenerateUtil.aliasGenerate(PaymentBillModel.class, "upReceiptBill", null),
              "statement.CODE AS statementOfAccountCode",
              "upReceiptBill.OTHER_PARTY_ACCOUNT_BANK_CODE AS ourBankInfoAccount",
              "upReceiptBill.OUR_ACCOUNT_BANK_CODE AS otherPartyBankInfoAccount");
      FROM("T_FINANCE_PAYMENT_BILL AS upReceiptBill");
      INNER_JOIN("T_FINANCE_STATEMENT_ACCOUNT_BILL AS statement ON statement.ID = upReceiptBill.STATEMENT_OF_ACCOUNT_ID");
    }};
  }

  @Override
  protected Class getViewObject() {
    return UpReceiptBillVo.class;
  }

}
